package com.park.platform.service.impl;

import com.park.common.bean.GoodInputView;
import com.park.common.bean.PageBean;
import com.park.common.constant.IDBConstant;
import com.park.common.exception.MessageException;
import com.park.common.poo.ActivityInfo;
import com.park.common.util.DateUtil;
import com.park.common.util.FileUtil;
import com.park.common.util.StrUtil;
import com.park.platform.dao.IBaseDao;
import com.park.platform.service.IActivityService;
import com.park.platform.service.IGoodService;
import com.park.platform.service.IMemberService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import java.io.IOException;
import java.util.List;
import java.util.Map;

@Service
public class GoodServiceImpl extends BaseService implements IGoodService {

	@Autowired
	private IBaseDao baseDao;
	
	@Autowired
	private IMemberService memberService;

	@Autowired
	private IActivityService activityService;
	
	@Override
	public Integer saveGood(ActivityInfo activityInfo, MultipartHttpServletRequest multipartRequest) throws IOException {
		Integer activityId = activityInfo.getActivityId();
		boolean newFlag = activityId == null;
		String nowDate = DateUtil.getNowDate();
		if(StrUtil.isBlank(activityInfo.getActivityBody())) throw new MessageException("请填写活动描述");
		if(activityId == null){ //添加
			activityInfo.setActivityPic(StrUtil.EMPTY);
			activityInfo.setActivityStatus(IDBConstant.LOGIC_STATUS_YES);
			activityInfo.setCreateTime(nowDate);
			baseDao.save(activityInfo, null);
			activityId = activityInfo.getActivityId();
		}else{ //修改
			ActivityInfo activityInfoDB = activityService.getActivityInfo(activityId);
			if(activityInfoDB == null) throw new MessageException("活动信息不存在！");
			activityInfoDB.setActivityName(activityInfo.getActivityName());
			activityInfoDB.setActivityBody(activityInfo.getActivityBody());
			activityInfoDB.setActivityAddress(activityInfo.getActivityAddress());
			activityInfoDB.setActivityInventory(activityInfo.getActivityInventory());
			activityInfoDB.setActivityLatitude(activityInfo.getActivityLatitude());
			activityInfoDB.setActivityLongitude(activityInfo.getActivityLongitude());
			activityInfoDB.setActivityNotice(activityInfo.getActivityNotice());
			activityInfoDB.setActivityPrice(activityInfo.getActivityPrice());
			activityInfoDB.setActivitySeq(activityInfo.getActivitySeq());
			activityInfoDB.setActivityTitle(activityInfo.getActivityTitle());
			activityInfoDB.setUpdateTime(nowDate);
			baseDao.save(activityInfoDB, activityInfoDB.getActivityId());
			activityId = activityInfoDB.getActivityId();
		}
		List<MultipartFile> multipartFiles = FileUtil.getMultipartFiles(multipartRequest);
		if(multipartFiles.size() > 0){ //保存活动图腾图片
			ActivityInfo activityInfoDB = activityService.getActivityInfo(activityId);
			activityInfoDB.setActivityPic(FileUtil.saveFile(multipartFiles.get(0), multipartRequest).toString());
			baseDao.save(activityInfoDB, activityInfoDB.getActivityId());
		}else if(newFlag){
			throw new MessageException("请上传活动图腾图片");
		}
		return activityId;
	}

	@Override
	public void newDeleteActivity(int activityId){
		ActivityInfo activityInfo = activityService.getActivityInfo(activityId);
		if(IDBConstant.LOGIC_STATUS_NO.equals(activityInfo.getActivityStatus())){
			baseDao.delete(activityInfo);
		}else {
			activityInfo.setActivityStatus(IDBConstant.LOGIC_STATUS_NO);
			baseDao.save(activityInfo, activityInfo.getActivityId());
		}
	}

	@Override
	public PageBean getGoods(GoodInputView goodInputView){
		String goodName = goodInputView.getGoodName();
		String goodStatus = goodInputView.getGoodStatus();
		
		StringBuilder headSql = new StringBuilder("SELECT *");
		StringBuilder bodySql = new StringBuilder(" FROM activity_info ai");
		StringBuilder whereSql = new StringBuilder(" WHERE 1=1");
		if(StrUtil.isNotBlank(goodName)){
			whereSql.append(" AND activityName LIKE :goodName");
			goodInputView.setGoodName(goodName + "%");
		}
		if(StrUtil.isNotBlank(goodStatus)){
			whereSql.append(" AND activityStatus = :goodStatus");
		}
        whereSql.append(" ORDER BY ai.createTime DESC");

		PageBean pageBean = super.getPageBean(headSql, bodySql, whereSql, goodInputView);
		List<Map<String, Object>> list = pageBean.getList();
		for(Map<String, Object> map : list){
			map.put("activityPic", FileUtil.getImgHttp(StrUtil.objToStr(map.get("activityPic"))));
		}

		return pageBean;
	}
	
}





